import request,{
      IP
} from '../utils/request.js';
import * as actionTypes from './actionTypes.js';
import { message } from 'antd';
export default function httpRequest(params) {
      const IP_URL = '/api/server/' + params.api

      return (dispatch) => {
            request("POST", IP_URL, {
                  cmd: params.cmd,
                  type: "request",
                  request: {
                        ...params.request
                  }
            }).then(response => {
                  const result = response.response;
                  if (result.res) {

                        if (params.method === "loginRequest") {
                              sessionStorage.setItem(
                                    "isLogin",
                                    JSON.stringify(result.res)
                              );
                              window.location.href='/'
                        }
                        console.log(result,'res')

                        dispatch(eval(params.method + "(result)"));
                  } else {
                        message.error(result.exception, 1.5);
                  }

            }).catch(error => { console.log(error) });
      };
};
export const isloginRequest = (params) => {
      console.log(params)
      const IP_URL = '/api/server/' + params.api
      console.log(IP_URL, 'ip')
      return (dispatch) => {
            request("POST", IP_URL, {
                  cmd: params.cmd,
                  type: "request",
                  request: {
                        ...params.request
                  }
            }).then(response => {
                  const result = response.response;
                  console.log("result",result)
                  if (result.res) {
                        sessionStorage.setItem(
                              "islogin",
                              JSON.stringify(result.res)
                        );
                        console.log(result.res, 'res')
                        // dispatch(eval(params.method + "(result)"));
                  } else {
                        // message.error(result.exception, 1.5);
                        sessionStorage.setItem("islogin", JSON.stringify(false));
                  }

            }).catch(error => {
                  console.log(error)
            });
      };
};

export const logoutRequest = (params) => {
      // const IP_URL = process.env.REMOTE_SERVER_IP + params.api
      return (dispatch) => {
            request("POST", IP + 'web/rest/user/logout', {
                  cmd: "logout",
                  type: "request",
                  request: {

                  }
            }).then(response => {
                  const result = response.response;
                  if (result.res) {
                        message.success('账号注销成功', 1);
                        sessionStorage.clear();
                        dispatch(logoutReducer(result.message))
                  } else {
                        message.error('账号注销失败' + result.exception, 1.5);
                  }
            }).catch(error => {
                  console.log(error)
            });
      };
};

// 登录相关
const loginRequest = result => ({ type: actionTypes.LOGIN, result });

// 忘记密码相关
const forgetPasswordRequest = result => ({ type: actionTypes.FORGETPASSWORD, result });

// 注册相关
const registerRequest = result => ({ type: actionTypes.REGISTER, result });

// 验证码相关
const validateRequest = result => ({ type: actionTypes.VALIDATE, result });

//  获取短信邮箱验证码相关
const phone_emailRequest = result => { return }

// //  获取验证码或更新验证码
// export const getValidate = () => {
//       const IP_URL = process.env.REMOTE_SERVER_IP  + params.api
// }

//账号注销
const logoutReducer = (result) => ({
      type: actionTypes.LOGOUT,
      result
})